package com.house.api.config;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.region.Region;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class MyCOSClient {

    @Value("${tencent.cos.secretId}")
    public String secretId;

    @Value("${tencent.cos.secretKey}")
    public String secretKey;

    @Value("${tencent.cos.buckerName}")
    public String buckerName;

    @Value("${tencent.cos.region}")
    public String region;

    @Value("${tencent.cos.url}")
    public String url;

    @Bean
    public COSClient cosClient() {
        // 1 初始化用户身份信息（secretId, secretKey）
        BasicCOSCredentials credentials = new BasicCOSCredentials(secretId, secretKey);
        // 2 设置 bucket 的区域, COS 地域的简称请参照
        Region region1 = new Region(region);
        ClientConfig clientConfig = new ClientConfig(region1);
        // 从 5.6.54 版本开始，默认使用了 https
        clientConfig.setHttpProtocol(HttpProtocol.https);
        // 3 生成 cos 客户端。
        return new COSClient(credentials, clientConfig);
    }
}
